package com.bwie.service.impl;

import com.bwie.exception.BusinessException;
import com.bwie.mapper.BillMapper;
import com.bwie.mapper.CarMonthMapper;
import com.bwie.mapper.MonthCarMapper;
import com.bwie.mapper.UserMapper;
import com.bwie.pojo.dto.CarMonthDto;
import com.bwie.pojo.po.Bill;
import com.bwie.pojo.po.CarMonth;
import com.bwie.pojo.po.MonthCar;
import com.bwie.pojo.po.User;
import com.bwie.pojo.vo.CarMonthVo;
import com.bwie.service.CarMonthService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class CarMonthServiceImpl implements CarMonthService {
    @Autowired
    private CarMonthMapper carMonthMapper;

    @Autowired
    private HttpSession session;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BillMapper billMapper;
    @Autowired
    private MonthCarMapper monthCarMapper;
    @Override
    @Transactional
    public void updCarMonth(CarMonth carMonth) {


        /**
         * 判断余额是否充裕
         */
        BigDecimal monthPrice = carMonth.getMonthPrice();

        User user = (User) session.getAttribute("user");
        BigDecimal userMoney = user.getUserMoney();

        int i = userMoney.compareTo(monthPrice);
        if (i<0){
            throw  new BusinessException("余额不足，请及时充值");
        }

        /**
         * 判断是否支付过
         */
        if (carMonth.getRechargeState()==1){
            throw new BusinessException("已支付过该订单不能重复支付");
        }


        //扣减余额
        userMapper.updMoney(user.getUserId(),monthPrice);



        //保存交易流水表
        Bill bill = new Bill();
        bill.setBillMoney(monthPrice);
        bill.setUserId(user.getUserId());
        bill.setBillTime(new Date());
        bill.setBillType(1);
        bill.setParkId(carMonth.getParkId());
        bill.setOrderParkId(carMonth.getId());
        billMapper.addBill(bill);


        //修改状态为已支付
        carMonthMapper.updCarMonth(carMonth);


        //将月卡相关信息保存到车辆月卡关联表
        MonthCar monthCar = new MonthCar();
        monthCar.setUserId(user.getUserId());
        monthCar.setMonthId(carMonth.getMonthId());
        monthCar.setCarCard(carMonth.getCarCard());
        monthCar.setParkId(carMonth.getParkId());
        monthCar.setUseTimes(carMonth.getUseTimes());
        monthCar.setUseState(0);
        monthCar.setStartTime(new Date());
        monthCarMapper.addMonthCar(monthCar);
    }

    @Override
    public PageInfo<CarMonthVo> getCarMonth(CarMonthDto carMonthDto) {
        PageHelper.startPage(carMonthDto.getPageNum(),carMonthDto.getPageSize());
        List<CarMonthVo> list = carMonthMapper.getCarMonth(carMonthDto);

        PageInfo<CarMonthVo> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }
}
